package cn.edu.seu.ssny.controller.admin;

import cn.edu.seu.ssny.common.Constants;
import cn.edu.seu.ssny.common.JsonResult;
import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.controller.admin.system.UserController;
import cn.edu.seu.ssny.entity.Project;
import cn.edu.seu.ssny.service.IProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

@Controller
@RequestMapping("/")
public class UploadController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(UploadController.class);
    //上传真实地址
    @Value("${file.uploadFolder}")
    private String uploadFolder;
    //映射地址
    @Value("${file.url}")
    private String url;

    @Autowired
    private IProjectService projectService;

    /*file upload*/
    @RequestMapping(value = "/picUpload", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult springUpload(HttpSession session, @RequestParam(value = "file") MultipartFile file, HttpServletRequest request, @RequestParam(required = false, value = "type") String type) throws IllegalStateException, IOException {
        long suffix = System.currentTimeMillis();
        try {
            //pic
            //String path="E:/springUpload"+file.getOriginalFilename();
            String path = uploadFolder + suffix;
            //上传
            file.transferTo(new File(path));
            System.out.println(request.getContextPath() + url + suffix);

            if (type != null) {
                Project project = getCurrProject(session);
                if (type.equals("image")) {//项目底图
                    project.setImage(url+suffix);
                } else if (type.equals("panel")) {//项目图片
                    project.setPanel(url+suffix);
                }
                projectService.saveOrUpdate(project, null);

            }

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success(request.getContextPath() + "/pic/" + suffix);
    }

}
